//
//  NewFeatureViewController.swift
//  SYLJWeibo
//
//  Created by 刘俊 on 15/10/17.
//  Copyright © 2015年 刘俊. All rights reserved.
//

import UIKit
import SnapKit

/// cell可重用标识符
private let NewFeatureViewCellId = "NewFeatureViewCellId"
/// cell的数量
private let NewFeatureViewCellCount = 4

class NewFeatureViewController: UICollectionViewController {

    // MARK: - 构造函数
    init () {
        let layout = UICollectionViewFlowLayout()
        layout.itemSize = UIScreen.mainScreen().bounds.size
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = 0
        layout.scrollDirection = .Horizontal
        
        super.init(collectionViewLayout: layout)
        
        collectionView?.bounces = false
        collectionView?.pagingEnabled = true
        collectionView?.showsHorizontalScrollIndicator = false
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - 页面布局
    override func viewDidLoad() {
        super.viewDidLoad()

        self.collectionView!.registerClass(NewFeatureViewCell.self, forCellWithReuseIdentifier: NewFeatureViewCellId)
    }
    // MARK: UICollectionViewDataSource
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return NewFeatureViewCellCount
    }

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(NewFeatureViewCellId, forIndexPath: indexPath) as! NewFeatureViewCell
//        cell.backgroundColor = UIColor.orangeColor()
        cell.iconImageIndex = indexPath.item
        return cell
    }

    // MARK: UICollectionViewDelegate
    /// collectionView完成滚动后调用
   override func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
    
        //因此按钮
        let pageCount = Int(scrollView.contentOffset.x / scrollView.bounds.width)
        let indextPath = NSIndexPath(forItem: pageCount, inSection: 0)
        let cell = collectionView?.cellForItemAtIndexPath(indextPath) as! NewFeatureViewCell
        
    if pageCount != NewFeatureViewCellCount - 1 {
        
        return
    }
        cell.showButtonAnim()
    }
    // MARK - 隐藏状态栏
    override func prefersStatusBarHidden() -> Bool {
        return true
    }

}

// MARK - 新特性Cell
private class NewFeatureViewCell: UICollectionViewCell {
    /// 新特性图片
    private var iconImageIndex:Int = 0 {
        didSet{
            iconView.image = UIImage(named: "new_feature_\(iconImageIndex + 1)")
            startButton.hidden = true
        }
    }
    /// 开始体验按钮动画
    private func showButtonAnim() {
        startButton.hidden = false
        startButton.userInteractionEnabled = false
        startButton.transform = CGAffineTransformMakeScale(0, 0)
        UIView.animateWithDuration(1.6, delay: 0, usingSpringWithDamping: 0.6, initialSpringVelocity: 10, options: [], animations: { () -> Void in
            self.startButton.transform = CGAffineTransformIdentity
            }) { (_) -> Void in
                self.startButton.userInteractionEnabled = true
        }
    }
    /// 重写对应的构造方法
    override init(frame: CGRect) {
        super.init(frame: frame)
        /// 设置item
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    /// 设置item
    private func setupUI() {
        addSubview(iconView)
        iconView.frame = self.bounds
        addSubview(startButton)
        startButton.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: UIControlState.Highlighted)
        startButton.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.snp_centerX)
            make.bottom.equalTo(self.snp_bottom).multipliedBy(0.7)
        }
        startButton.addTarget(self, action: "clickStartBtn", forControlEvents: UIControlEvents.TouchUpInside)
    }
    
    @objc private func clickStartBtn() {
        NSNotificationCenter.defaultCenter().postNotificationName(SYLJSwitchRootViewControllerNotification, object: nil)
    }
    // MARK - 懒加载控件
    /// 新特性图片
    private lazy var iconView: UIImageView = UIImageView()
    /// 开始体验按钮
    private lazy var startButton: UIButton = UIButton(title: "开始体验", color: UIColor.whiteColor(), backImageName: "new_feature_finish_button")
}